package com.winhxd.b2c.order.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.context.CustomerUser;
import com.winhxd.b2c.common.context.UserContext;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.order.condition.ShopCartEditCondition;
import com.winhxd.b2c.common.domain.order.condition.ShopCartQueryCondition;
import com.winhxd.b2c.common.domain.order.condition.ShopCartRemoveCondition;
import com.winhxd.b2c.common.domain.order.vo.ShopCartVO;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.order.service.OrderShopCartService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 购物车
 * @author suhaochan
 * @date 2019/1/7 19:25:07
 */
@RestController
@Api(tags = "ApiOrderShopCartController")
@RequestMapping(value = "/api-order/order")
public class ApiOrderShopCartController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ApiOrderShopCartController.class);

	//购物车单个产品最大购买数量
	private static final int SHOPCART_MAX_NUM = 99;
	
    @Autowired
    private OrderShopCartService orderShopCartService;
    
    @ApiOperation(value = "查询购物车",response = ShopCartVO.class, notes = "查询购物车")
    @RequestMapping(value = "/4030/v1/findShopCart", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<PagedList<ShopCartVO>> findShopCart(@RequestBody ShopCartQueryCondition condition){
    	String logTitle = "/api-order/order/4030/v1/findShopCart-查询购物车";
    	LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
    	ResponseResult<PagedList<ShopCartVO>> result = new ResponseResult<>();
    	CustomerUser customer = UserContext.getCurrentCustomerUser();
        if(customer == null || customer.getCustomerId() == null){
            LOGGER.error(logTitle+"--用户为空！");
            throw new BusinessException(BusinessCode.CODE_403001);
        }
    	condition.setCustomerId(customer.getCustomerId());
    	//默认从第1页开始查询，每页100条
    	if (condition.getPageNo() == 0) {
    		condition.setPageNo(1);
    	}
    	if (condition.getPageSize() == 0) {
    		condition.setPageSize(100);
    	}
    	PagedList<ShopCartVO> data = orderShopCartService.findShopCart(condition);
    	result.setData(data);
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }

    @ApiOperation(value = "添加购物车",response = Long.class, notes = "添加购物车")
    @RequestMapping(value = "/4031/v1/saveShopCart", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Long> saveShopCart(@RequestBody ShopCartEditCondition condition){
    	String logTitle = "/api-order/order/4031/v1/saveShopCart-添加购物车";
    	LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
    	ResponseResult<Long> result = new ResponseResult<>();
    	CustomerUser customer = UserContext.getCurrentCustomerUser();
    	if(customer == null || customer.getCustomerId() == null){
    	    LOGGER.error(logTitle+"--用户为空！");
    	    throw new BusinessException(BusinessCode.CODE_403001);
    	}
    	condition.setCustomerId(customer.getCustomerId());
        if (condition.getSpuId() == null || condition.getSkuId() == null || condition.getNum() == null) {
        	throw new BusinessException(BusinessCode.CODE_403003);
        }
        if (condition.getNum() > SHOPCART_MAX_NUM){
			throw new BusinessException(BusinessCode.CODE_403008);
		}
        int data = orderShopCartService.saveShopCart(condition);
        result.setData(new Long(data));
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }

    @ApiOperation(value = "删除或清空购物车",response = Long.class, notes = "删除或清空购物车")
    @RequestMapping(value = "/4032/v1/removeShopCart", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Long> removeShopCart(@RequestBody ShopCartRemoveCondition condition){
    	String logTitle = "/api-order/order/4032/v1/removeShopCart-删除/清空购物车";
    	LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
    	ResponseResult<Long> result = new ResponseResult<>();
    	CustomerUser customer = UserContext.getCurrentCustomerUser();
    	if(customer == null || customer.getCustomerId() == null){
    	    LOGGER.error(logTitle+"--用户为空！");
    	    throw new BusinessException(BusinessCode.CODE_403001);
    	}
    	condition.setCustomerId(customer.getCustomerId());
    	int data = orderShopCartService.removeShopCart(condition);
    	result.setData(new Long(data));
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }

    @ApiOperation(value = "修改购物车",response = Long.class, notes = "修改购物车")
    @RequestMapping(value = "/4033/v1/editShopCart", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Long> editShopCart(@RequestBody ShopCartEditCondition condition){
    	String logTitle = "/api-order/order/4033/v1/editShopCart-修改购物车";
    	LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
    	ResponseResult<Long> result = new ResponseResult<>();
    	CustomerUser customer = UserContext.getCurrentCustomerUser();
    	if(customer == null || customer.getCustomerId() == null){
    	    LOGGER.error(logTitle+"--用户为空！");
    	    throw new BusinessException(BusinessCode.CODE_403001);
    	}
    	condition.setCustomerId(customer.getCustomerId());
        if (condition.getShopCarId() == null || condition.getNum() == null || condition.getNum() <= 0) {
        	throw new BusinessException(BusinessCode.CODE_403006);
        }
		if (condition.getNum() > SHOPCART_MAX_NUM){
			throw new BusinessException(BusinessCode.CODE_403008);
		}
        int data = orderShopCartService.modifyShopCart(condition);
        result.setData(new Long(data));
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }
    
    @ApiOperation(value = "查询购物车商品数量",response = Long.class, notes = "查询购物车商品数量")
    @RequestMapping(value = "/4034/v1/getShopCartProductNum", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Long> getShopCartProductNum(@RequestBody ShopCartQueryCondition condition){
    	String logTitle = "/api-order/order/4034/v1/getShopCartProductNum-查询购物车商品数量";
    	LOGGER.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
    	ResponseResult<Long> result = new ResponseResult<>();
    	CustomerUser customer = UserContext.getCurrentCustomerUser();
    	if(customer == null || customer.getCustomerId() == null){
    	    LOGGER.error(logTitle+"--用户为空！");
    	    throw new BusinessException(BusinessCode.CODE_403001);
    	}
    	condition.setCustomerId(customer.getCustomerId());
        int data = orderShopCartService.getShopCartProductTypeNum(condition);
        result.setData(new Long(data));
        LOGGER.info("{}=--结束 result={}", logTitle, result);
        return result;
    }
    
}
